﻿using CommonDevelop;
using CommonDevelop.Scene;
using CommonEngine;
using CommonEngine.Power;
using ManageService;
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebAdmin
{
    /// <summary>
    /// 全局变量池
    /// </summary>
    public sealed class GlobalData
    {
        /// <summary>
        /// 配置服务固定菜单数据
        /// </summary>
        public static List<SysMenu> MenuLs { get; private set; }
        internal static readonly object padlock = new object();
        public static string DomainPath { get; set; }

        /// <summary>
        /// 为Admin加载系统菜单
        /// </summary>
        private static void InitMenuLs()
        {
            //---菜单数据---
            if (MenuLs == null)
            {
                lock (padlock)
                {
                    if (MenuLs == null)
                    {
                        MenuLs = new List<SysMenu>();
                        SysMenu Node10 = new SysMenu { Module_Code = "SysM010", Module_Name = "系统管理", Module_Icon = "fa-cubes", Module_Action = "#", Parent_Id = "" };
                        MenuLs.Add(Node10);
                        MenuLs.Add(new SysMenu { Module_Code = "SysM011", Module_Name = "菜单模块", Module_Icon = "fa-cogs", Module_Action = "/Org/Module/Index", Parent_Id = Node10.Module_Id });

                        SysMenu Node20 = new SysMenu { Module_Code = "SysM020", Module_Name = "组织管理", Module_Icon = "fa-cubes", Module_Action = "#", Parent_Id = "" };
                        MenuLs.Add(Node20);
                        MenuLs.Add(new SysMenu { Module_Code = "SysM021", Module_Name = "公司管理", Module_Icon = "fa-globe", Module_Action = "/Org/Company/Index", Parent_Id = Node20.Module_Id });
                        MenuLs.Add(new SysMenu { Module_Code = "SysM022", Module_Name = "部门管理", Module_Icon = "fa-users", Module_Action = "/Org/Dept/Index", Parent_Id = Node20.Module_Id });
                        MenuLs.Add(new SysMenu { Module_Code = "SysM023", Module_Name = "岗位管理", Module_Icon = "fa-user-circle", Module_Action = "/Org/Position/Index", Parent_Id = Node20.Module_Id });
                        MenuLs.Add(new SysMenu { Module_Code = "SysM024", Module_Name = "角色管理", Module_Icon = "fa-user-o", Module_Action = "/Org/Role/Index", Parent_Id = Node20.Module_Id });
                        MenuLs.Add(new SysMenu { Module_Code = "SysM025", Module_Name = "组织架构", Module_Icon = "fa-building", Module_Action = "/Org/OrgUser/Index", Parent_Id = Node20.Module_Id });
                        MenuLs.Add(new SysMenu { Module_Code = "SysM026", Module_Name = "权限配置", Module_Icon = "fa-share-alt-square", Module_Action = "/Org/Power/Index", Parent_Id = Node20.Module_Id });
                    }
                    MenuLs.ForEach(x => x.Module_Address = "#");
                }
            }
        }

        /// <summary>
        /// 内存初始化
        /// </summary>
        public static void Init()
        {
            InitMenuLs();

            IUserService UR = IocContainer.Resolve<IUserService>();
            System.Diagnostics.Debug.WriteLine(UR.Init_Admin());
        }

        /// <summary>
        /// 取菜单配置
        /// </summary>
        public static List<SysMenu> GetMenu(UserInfo UserObj)
        {
            List<SysMenu> tableData = new List<SysMenu>();
            if (UserObj.MyMenu != null)
                tableData.AddRange(UserObj.MyMenu.ToArray());

            if (UserObj.IsAdmin)
            {
                InitMenuLs();
                tableData.AddRange(MenuLs);
            }
            return tableData;
        }

        /// <summary>
        /// UI权限判断
        /// </summary>
        /// <param name="IsAdmin">LoginHelper.Instance.Current.IsAdmin</param>
        /// <param name="Power">LoginHelper.Instance.Current.Power</param>
        /// <param name="RID">资源.TypeId</param>
        /// <param name="op">(int)资源.Operation.Edit</param>
        /// <returns></returns>
        public static bool UI_Authority(bool IsAdmin, List<ClientPower> Power, Guid RID, int op)
        {
            if (IsAdmin)
                return true;

            if (Power == null || Power.Count < 1)
                return false;

            var cp = Power.FirstOrDefault(a => a.ResourceId == RID.ToString());
            if (cp == null)
                return false;

            if (PragmaticAction.Has_Rights(cp.Grant, op))
                return true;
            else
                return false;
        }
    }
}
